package day10.shop;

import java.io.*;
import java.util.*;

/**
 * @author oldliu
 * @since 1.0
 */
public class Shop implements Serializable {
    private String name;
    private String adr;
    private Map<Integer, Card> cardMap = new HashMap<>();
    private Map<String, Double> shangpin = new HashMap<>();

    public static final String OBJFILE="shop.obj";
    //从文件中读取对象
    public static Shop getFromFile(){
        try {
            ObjectInputStream oin = new ObjectInputStream(new FileInputStream(OBJFILE));
            Shop a = (Shop) oin.readObject();
            oin.close();
            return a;
        } catch (Exception e) {
            //System.out.println("对象不在");
           e.printStackTrace();
        }
        return null;
    }
    public static void storeObj(Shop s){
        try {
            ObjectOutputStream oout=new ObjectOutputStream(new FileOutputStream(OBJFILE));
            oout.writeObject(s);
            oout.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public Shop() {
        shangpin.put("餐巾纸", 100.0);
        shangpin.put("剃须刀", 500.0);
        shangpin.put("汽车", 50000.0);
    }

    public boolean objectOUtputStream(Shop a, File file) {
        try {
            ObjectOutputStream b = new ObjectOutputStream(new FileOutputStream(file));
            b.writeObject(a);
            b.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    public boolean add(Card a) {

        if (cardMap.size() == 0) {
            cardMap.put(a.getNumber(), a);
            return true;
        }
        if (this.checkNumber(a.getNumber())) {
            return false;
        }
        cardMap.put(a.getNumber(), a);
        return true;

    }

    public boolean addfen(int number, double fen) {
        if (this.checkNumber(number)) {
            Card b = this.xunka(number);
            b.setFen(b.getFen() + fen);
            return true;
        }
        System.out.println("卡号错误");
        return false;
    }

    public boolean diuhuan(int number, String password, String shangpin) {
        if (this.checkPassword(number, password) && this.checkShangpin(shangpin)) {
            Card b = this.xunka(number);
            if (b.getFen() >= this.shangpin.get(shangpin)) {
                b.setFen(b.getFen() - this.shangpin.get(shangpin));
                return true;
            }
            return false;
        }
        return false;
    }

    public double findFen(int number, String password) {
        if (checkPassword(number, password)) {
            Card b = this.xunka(number);

            return b.getFen();
        }
        return -1;
    }

    public boolean changePassword(int number, String password, String newpassword) {
        if (this.checkPassword(number, password)) {
            Card b = this.xunka(number);
            b.setPassword(newpassword);
            return true;
        }
        return false;
    }

    public Card xunka(int number) {
        return cardMap.get(number);
    }

    public boolean checkNumber(int number) {
        if (cardMap.containsKey(number)) {
            return true;
        }
        return false;
    }

    public boolean checkPassword(int number, String password) {
        if (this.checkNumber(number)) {
            Card b = this.xunka(number);
            if (b.getPassword().equals(password)) {
                return true;
            }
            System.out.println("密码错误");
            return false;
        }
        System.out.println("账号错误");
        return false;
    }

    public boolean checkShangpin(String Shangpin) {
        if (this.shangpin.containsKey(Shangpin)) {
            return true;
        }
        System.out.println("商品不存在");
        return false;

    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAdr() {
        return adr;
    }

    public void setAdr(String adr) {
        this.adr = adr;
    }

    public Map<Integer, Card> getCardMap() {
        return cardMap;
    }

    public void setCardMap(Map<Integer, Card> cardMap) {
        this.cardMap = cardMap;
    }

    public void paixu(int number) {
        if (cardMap.size() == 0) {
            return;
        }

        Set<Map.Entry<Integer, Card>> set = cardMap.entrySet();
        Iterator<Map.Entry<Integer, Card>> in = set.iterator();
        LinkedList<Card> a = new LinkedList<>();
        while (in.hasNext()) {
            Map.Entry<Integer, Card> e = in.next();
            a.add(e.getValue());
        }
        Collections.sort(a);
        System.out.println(a);

    }
}
